package ace.module.oauth2.server.core.impl.converter.impl;

import ace.module.oauth2.server.core.impl.converter.AuthorizationGrantTypeToStringConverter;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/10/12 11:50
 */
@Component
public class AuthorizationGrantTypeToStringConverterImpl
    implements AuthorizationGrantTypeToStringConverter {

  @Override
  public String convert(AuthorizationGrantType source) {
    return Optional.ofNullable(source).map(p -> p.getValue()).orElse(StringUtils.EMPTY);
  }

  @Override
  public Set<String> convert(Set<AuthorizationGrantType> sources) {
    return Stream.ofNullable(sources)
        .flatMap(Set::stream)
        .map(p -> this.convert(p))
        .collect(Collectors.toSet());
  }

  @Override
  public String convertToString(Set<AuthorizationGrantType> sources) {
    return Stream.ofNullable(sources)
        .flatMap(Set::stream)
        .map(p -> this.convert(p))
        .collect(Collectors.joining(","));
  }
}
